home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9675 / 9675.xpi / chrome / content / simpletimer-eventLog.js < prev    next >
Encoding:
JavaScript  |  2009-11-23  |  11.2 KB  |  307 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Simple Timer.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  *   George Bradt.
  18.  *
  19.  * Portions created by the Initial Developer are Copyright (C) 2009
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Credits:
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. var SimpleTimerEventLog = {
  41.     separator: "\x1D",
  42.  
  43.     // Called when loading Event Log dialog.
  44.  
  45.     onLoadEventLog: function() {
  46.         var strbundle = document.getElementById("simtim-strings");
  47.  
  48.         try {
  49.             var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  50.                     getService(Components.interfaces.nsIPrefService).
  51.                     getBranch("extensions.simpletimer@grbradt.org.");
  52.             var eventLogPath = prefs.getComplexValue("eventLogPath",
  53.                     Components.interfaces.nsILocalFile).path;
  54.             var deleteLogEntries = prefs.getIntPref("deleteLogEntries");
  55.         }
  56.         catch(e) {
  57.             alert(strbundle.getString("alert.error.get.prefs") +
  58.                     "\n" + e.message);
  59.             return;
  60.         }
  61.  
  62.  
  63.         var json = Components.classes["@mozilla.org/dom/json;1"].
  64.                 createInstance(Components.interfaces.nsIJSON);
  65.  
  66.         var strArray = [];
  67.         var logArray = [];
  68.         var file = SimpleTimerFileIO.open(eventLogPath);
  69.  
  70.         if ( file && file.exists() ) {
  71.             var treeChildren = document.getElementById("simtim-treeItems");
  72.             var inData = SimpleTimerFileIO.read(file);
  73.  
  74.             if ( inData ) {
  75.                 // Calculate entry delete time. deleteLogEntries is days to retain an entry,
  76.                 // and a value of 0 means no deleting.
  77.                 if ( deleteLogEntries ) {
  78.                     var currTimeMilli = new Date().getTime();
  79.                     var lifeSpanMilli = deleteLogEntries * ( 24 * 60 * 60 * 1000 );
  80.                 }
  81.  
  82.                 // Strip last seperator.
  83.                 inData = inData.slice(0, -1);
  84.  
  85.                 // An array of JSON strings.
  86.                 strArray = inData.split(this.separator);
  87.  
  88.                 // Update log file if entries are dropped.
  89.                 var outData = "";
  90.                 var entriesDropped = false;
  91.  
  92.                 for ( var i in strArray ) {
  93.                     try {
  94.                         logArray[i] = json.decode(strArray[i]);
  95.                     }
  96.                     catch(e) {
  97.                         window.opener.alert(strbundle.getString("alert.error.log.invalid.data.in") + "\n" +
  98.                                 strArray[i]);
  99.                         continue;
  100.                     }
  101.  
  102.                     if ( !deleteLogEntries ||
  103.                        ( logArray[i].logTimeMilli + lifeSpanMilli ) > currTimeMilli ) {
  104.                         this.addTreeRow(treeChildren, logArray[i]);
  105.                         outData += strArray[i] + this.separator;
  106.                     }
  107.                     else {
  108.                         entriesDropped = true;
  109.                     }
  110.                 }
  111.  
  112.                 if ( entriesDropped ) {
  113.                     // Overwrite log file.
  114.                     if ( !SimpleTimerFileIO.write(file, outData, "") ) {
  115.                         window.opener.alert(strbundle.getString("alert.error.log.file.write") + "\n" +
  116.                                 eventLogPath);
  117.                     }
  118.                 }
  119.             }
  120.             // Empty log file also drops here.
  121.             else if ( inData === false ) {
  122.                 window.opener.alert(strbundle.getString("alert.error.log.file.read") + "\n" +
  123.                         eventLogPath);
  124.             }
  125.         }
  126.         else {
  127.             window.opener.alert(strbundle.getString("alert.error.log.file.open") + "\n" +
  128.                     eventLogPath);
  129.         }
  130.     },
  131.  
  132.     // Add a logged event to the tree.
  133.  
  134.     addTreeRow: function(treeChildren, data) {
  135.         var item = document.createElement("treeitem");
  136.         var row = document.createElement("treerow");
  137.         var cell;
  138.  
  139.         // First cell is "logDate".
  140.         cell = document.createElement("treecell");
  141.         cell.setAttribute("label", data.logDate);
  142.         row.appendChild(cell);
  143.  
  144.         // Second cell is "logTime".
  145.         cell = document.createElement("treecell");
  146.         cell.setAttribute("label", data.logTime);
  147.         row.appendChild(cell);
  148.  
  149.         // Third cell is "type".
  150.         cell = document.createElement("treecell");
  151.         cell.setAttribute("label", data.type);
  152.         row.appendChild(cell);
  153.  
  154.         // Fourth cell is "eventTime".
  155.         cell = document.createElement("treecell");
  156.         cell.setAttribute("label", data.eventTime);
  157.         row.appendChild(cell);
  158.  
  159.         // Fifth cell is "recurring".
  160.         cell = document.createElement("treecell");
  161.         cell.setAttribute("label", data.recurring);
  162.         row.appendChild(cell);
  163.  
  164.         // Sixth cell is "status".
  165.         cell = document.createElement("treecell");
  166.         cell.setAttribute("label", data.status);
  167.         row.appendChild(cell);
  168.  
  169.         // Seventh cell is "descr", the optional description.
  170.         cell = document.createElement("treecell");
  171.         cell.setAttribute("label", data.description);
  172.         row.appendChild(cell);
  173.  
  174.         // Eighth cell is "url", the optional url for notifications.
  175.         cell = document.createElement("treecell");
  176.         cell.setAttribute("label", data.url);
  177.         row.appendChild(cell);
  178.  
  179.         // Add the treerow onto treeitem, append item.
  180.         item.appendChild(row);
  181.         treeChildren.appendChild(item);
  182.     },
  183.  
  184.     logEvent: function(type, eventTime, recurring, status, description, url) {
  185.         var strbundle = document.getElementById("simtim-strings");
  186.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
  187.                 getService(Components.interfaces.nsIWindowMediator);
  188.         var win = wm.getMostRecentWindow("navigator:browser");
  189.  
  190.         // Compare window references.
  191.         if ( win.window != window.window ) {
  192.             // Only log once.
  193.             return;
  194.         }
  195.  
  196.         var json = Components.classes["@mozilla.org/dom/json;1"].
  197.                 createInstance(Components.interfaces.nsIJSON);
  198.  
  199.         var file = SimpleTimerFileIO.open(SimpleTimer.eventLogPath);
  200.  
  201.         if ( file && file.exists() ) {
  202.             var currDate = new Date();
  203.  
  204.             var logData = {logDate: currDate.toLocaleDateString(),
  205.                            logTime: currDate.toLocaleTimeString(),
  206.                            logTimeMilli: currDate.getTime(),
  207.                            type: type,
  208.                            eventTime: eventTime,
  209.                            recurring: recurring,
  210.                            status: status,
  211.                            description: description,
  212.                            url: url
  213.                           };
  214.  
  215.             try {
  216.                 var data = json.encode(logData) + this.separator;
  217.             }
  218.             catch(e) {
  219.                 alert(strbundle.getString("alert.error.log.invalid.data.out") + "\n" +
  220.                         logData);
  221.                 return;
  222.             }
  223.  
  224.             // Append to log file.
  225.             if ( !SimpleTimerFileIO.write(file, data, "a") ) {
  226.                 alert(strbundle.getString("alert.error.log.file.write") + "\n" +
  227.                         eventLogPath);
  228.             }
  229.         }
  230.         else {
  231.             alert(strbundle.getString("alert.error.log.file.open") + "\n" +
  232.                     eventLogPath);
  233.         }
  234.     },
  235.  
  236.     // Called when clicking Clear button in Event Log dialog.
  237.  
  238.     onClear: function() {
  239.         var strbundle = document.getElementById("simtim-strings");
  240.         var file = SimpleTimerFileIO.open(window.opener.SimpleTimer.eventLogPath);
  241.  
  242.         if ( file && file.exists() ) {
  243.             var inData = SimpleTimerFileIO.read(file);
  244.  
  245.             if ( inData ) {
  246.                 // Get confirmation.
  247.                 var button = confirm(strbundle.getString("confirm.clear.log.file"));
  248.  
  249.                 if ( button ) {
  250.                     // User clicked OK. Overwrite log file.
  251.                     if ( !SimpleTimerFileIO.write(file, "", "") ) {
  252.                         alert(strbundle.getString("alert.error.log.file.write") + "\n" +
  253.                                 eventLogPath);
  254.                     }
  255.                     else {
  256.                         // Delete tree items.
  257.                         var treeChildren = document.getElementById("simtim-treeItems");
  258.  
  259.                         // Remove from the bottom.
  260.                         while ( treeChildren.hasChildNodes() ) {
  261.                             treeChildren.removeChild(treeChildren.lastChild);
  262.                         }
  263.                     }
  264.                 }
  265.             }
  266.             else if ( inData === false ) {
  267.                 alert(strbundle.getString("alert.error.log.file.read") + "\n" +
  268.                         eventLogPath);
  269.             }
  270.         }
  271.         else {
  272.             alert(strbundle.getString("alert.error.log.file.open") + "\n" +
  273.                     eventLogPath);
  274.         }
  275.     },
  276.  
  277.     // Called on first-run at startup, or when resetting from Options dialog.
  278.  
  279.     createDefaultLogFile: function() {
  280.         var strbundle = document.getElementById("simtim-strings");
  281.         var file = Components.classes["@mozilla.org/file/directory_service;1"].
  282.                 getService(Components.interfaces.nsIProperties).
  283.                 get("ProfD", Components.interfaces.nsIFile);
  284.  
  285.         file.append("SimpleTimer");
  286.         file.append("eventLog.txt");
  287.  
  288.         if ( !file.exists() ) {
  289.             if ( SimpleTimerFileIO.create(file) ) {
  290.                 SimpleTimer.prefs.setComplexValue("eventLogPath", Components.interfaces.nsILocalFile, file);
  291.             }
  292.             else {
  293.                 alert(strbundle.getString("alert.error.log.file.create") + "\n" +
  294.                         file.path);
  295.                 return false;
  296.             }
  297.         }
  298.  
  299.         return file;
  300.     },
  301.  
  302.     // Debug messages to console.
  303.  
  304.     debug: function(aMsg) {
  305.         setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
  306.     }
  307. };